package com.woniuxy.cloud.carl.marketing.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.cloud.Aspect.log.Log;
import com.woniuxy.cloud.carl.carlgoods.client.SkuQueryClient;
import com.woniuxy.cloud.carl.carlgoods.client.dto.SkuDto;
import com.woniuxy.cloud.carl.marketing.client.ScoreMallClient;
import com.woniuxy.cloud.carl.marketing.client.param.AddScoreMallParam;
import com.woniuxy.cloud.carl.marketing.client.param.ExchangeGoodsParam;
import com.woniuxy.cloud.carl.marketing.client.result.ScoreCouponDto;
import com.woniuxy.cloud.carl.marketing.client.result.ScoreLogDto;
import com.woniuxy.cloud.carl.marketing.client.result.ScoreMallGoodsDto;
import com.woniuxy.cloud.carl.marketing.client.result.ScoreSkuDto;
import com.woniuxy.cloud.carl.marketing.infra.model.Deduction;
import com.woniuxy.cloud.carl.marketing.infra.model.FullDiscountCoupon;
import com.woniuxy.cloud.carl.marketing.infra.model.ServiceCoupon;
import com.woniuxy.cloud.carl.marketing.service.*;
import com.woniuxy.cloud.carl.marketing.service.servicedto.ScoreCouponServiceDto;
import com.woniuxy.cloud.carl.marketing.service.servicedto.ScoreMallServiceDto;
import com.woniuxy.cloud.carl.marketing.service.servicedto.ScoreSkuServieDto;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@RestController
public class ScoreMallAPIImpl implements ScoreMallClient {
    @Autowired
    ScoreMallService scoreMallService;
    @Autowired
    CouponService couponService;
    @Autowired
    SkuQueryClient skuQueryClient;
    @Autowired
    DeductionService deductionService;
    @Autowired
    FullDiscountCouponService fullDiscountCouponService;
    @Autowired
    ServiceCouponService serviceCouponService;

    @Override
    @Log("上架积分商城商品")
    public Result addScoreMall(AddScoreMallParam param) {
        //调用sku接口查询商品是否存在
        if (param.getSkuId()!=null){
            Result<SkuDto> skuById = skuQueryClient.findSkuById(param.getSkuId());
            if (skuById.getData()==null) throw new MallException("商品信息错误");
        }
        return scoreMallService.addScoreMall(param);
    }

    @Override
    @Log("下架积分商城商品")
    public Result delScoreMall(Long scoreMallGoodsId) {

        return scoreMallService.delScoreMall(scoreMallGoodsId);
    }

    @Override
    @Log("兑换积分商品")
    public Result exchangeGoods(ExchangeGoodsParam param) {
        return scoreMallService.exchangeGoods(param);
    }
    @Override
    @Log("查询所有积分商城物品")
    public Result<ScoreMallGoodsDto> getScoreMall() {
        ScoreMallServiceDto scoreMallServiceDtocoreMall = scoreMallService.getScoreMall();
        ArrayList<ScoreSkuDto> scoreSkuDtos = new ArrayList<>();
        ArrayList<ScoreCouponDto> scoreCouponDtos = new ArrayList<>();
        List<ScoreCouponServiceDto> couponServiceDtos = scoreMallServiceDtocoreMall.getScoreCouponServiceDtos();
        for (ScoreCouponServiceDto couponServiceDto : couponServiceDtos) {
            ScoreCouponDto scoreCouponDto = BeanUtil.copyProperties(couponServiceDto, ScoreCouponDto.class);
            Deduction deduction = deductionService.getById(couponServiceDto.getCouponId());
            FullDiscountCoupon discountCoupon = fullDiscountCouponService.getById(couponServiceDto.getCouponId());
            ServiceCoupon serviceCoupon = serviceCouponService.getById(couponServiceDto.getCouponId());
            if (deduction!=null) {
                scoreCouponDto.setCouponImage(deduction.getImage());
                scoreCouponDto.setCouponName(deduction.getCouponName());
            }else if (discountCoupon!=null){
                scoreCouponDto.setCouponImage(discountCoupon.getImage());
                scoreCouponDto.setCouponName(discountCoupon.getCouponName());
            }else {
                scoreCouponDto.setCouponImage(serviceCoupon.getImage());
                scoreCouponDto.setCouponName(serviceCoupon.getCouponName2());
            }
            scoreCouponDtos.add(scoreCouponDto);
        }
        //调用skuclient根据skuids查询sku信息
        List<ScoreSkuServieDto> skuServieDtos = scoreMallServiceDtocoreMall.getScoreSkuServieDtos();
        for (ScoreSkuServieDto skuServieDto : skuServieDtos) {
            ScoreSkuDto scoreSkuDto = BeanUtil.copyProperties(skuServieDto, ScoreSkuDto.class);
            Result<SkuDto> skuById = skuQueryClient.findSkuById(skuServieDto.getSkuId());
            scoreSkuDto.setSkuDto(skuById.getData());
            scoreSkuDtos.add(scoreSkuDto);
        }
        ScoreMallGoodsDto scoreMallGoodsDto = new ScoreMallGoodsDto();
        scoreMallGoodsDto.setScoreSkuDtos(scoreSkuDtos);
        scoreMallGoodsDto.setScoreCouponDtos(scoreCouponDtos);
        return Result.success(scoreMallGoodsDto);
    }

    @Override
    @Log("查询用户积分兑换记录")
    public Result<List<ScoreLogDto>> getMyScoreLog(Long userId) {
        return scoreMallService.getMyScoreLog(userId);
    }
}
